require 'mysql'
require 'twitter'
require 'yaml'
require './HashTags.rb'

#load config
begin
    config      = YAML.load_file("twit.conf")
    con_key     = config["consumer_key"]
    con_sec     = config["consumer_secret"]
    oauth_tok   = config["oauth_token"]
    oauth_sec   = config["oauth_token_secret"]
    my_addr     = config["mysql_address"]
    my_user     = config["mysql_user"]
    my_pwd      = config["mysql_password"]
    my_db       = config["mysql_database"]
    
    puts "Config loaded."
rescue YAML::Error => e
    puts "Error loading YAML config."
    puts "Closing..."
    exit
end

#connect to twitter
begin
    Twitter.configure do |config|
       config.consumer_key         = con_key
       config.consumer_secret      = con_sec
       config.oauth_token          = oauth_tok
       config.oauth_token_secret   = oauth_sec
    end

    puts "Twitter configured."
rescue Twitter::Error => e
    puts "Error configuring Twitter."
    puts "Closing..."
    exit
end

#connect to mysql
begin
    mysql_con = Mysql.new(my_addr, my_user, my_pwd, my_db)
    
    puts "Connected to MySQL."
rescue Mysql::Error => e
    puts "Mysql error #{e.errno}: #{e.error}"
    puts "Closing..."
    exit
end

hashtags = HashTags.new(mysql_con)
hashtags.load()
htl = hashtags.get_hashtags()
htl.each { |ht| puts "id: #{ht.id} tag: #{ht.tag} state: #{ht.state}" }
hashtags.save()
